package com.yangyang.ralearn.client;

import java.util.List;

import org.springframework.stereotype.Component;

import com.yangyang.ralearn.common.entity.base.AbstractDomainPredicate;
import com.yangyang.ralearn.common.exception.ErrorActionException;
import com.yangyang.ralearn.common.exception.ReachGoalException;
import com.yangyang.ralearn.message.ReqCanDoMessage;
import com.yangyang.ralearn.message.ReqCreateGroupMessage;
import com.yangyang.ralearn.message.ReqDoActionMessage;
import com.yangyang.ralearn.message.ReqObserveMessage;
import com.yangyang.ralearn.message.RespCanDoMessage;
import com.yangyang.ralearn.message.RespCreateGroupMessage;
import com.yangyang.ralearn.message.RespDoActionMessage;
import com.yangyang.ralearn.message.RespObserveMessage;
import com.yangyang.ralearn.net.BaseClientSimulatorHandler;
import com.yangyang.ralearn.net.message.MessageFactory;
import com.yangyang.ralearn.net.message.MessageId;

@Component
public class RLClientHandler extends BaseClientSimulatorHandler {

	/**
	 * 重要，每一个实现客户端的操作必须赋值类
	 */

	@SuppressWarnings("unused")
	public void init(String domainFile, String problemFile,
			IClientEnvironmentListener listener) {
		ReqCreateGroupMessage message = (ReqCreateGroupMessage) MessageFactory
				.instance().createMessage(MessageId.REQ_CREATE_GROUP);
		message.init(domainFile, problemFile);
		RespCreateGroupMessage respMessage = (RespCreateGroupMessage) this
				.syncSendMessage(message, (Integer[]) null);
		listener.init(respMessage.getEnvironment(), respMessage.getGoalStates());
	}

	public List<AbstractDomainPredicate> observe() {
		ReqObserveMessage message = (ReqObserveMessage) MessageFactory
				.instance().createMessage(MessageId.REQ_OBSERVE);
		RespObserveMessage recvMessage = (RespObserveMessage) this
				.syncSendMessage(message, (Integer[]) null);
		return recvMessage.getCurrentStates();
	}

	public boolean canDo(String line) {
		ReqCanDoMessage message = (ReqCanDoMessage) MessageFactory.instance()
				.createMessage(MessageId.REQ_CANDO);
		message.init(line);
		RespCanDoMessage recvMessage = (RespCanDoMessage) this.syncSendMessage(
				message, (Integer[]) null);
		// try {
		// Thread.sleep(50);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		return recvMessage.getResult() == 1;
	}

	public void doAction(String line) throws ReachGoalException,
			ErrorActionException {
		ReqDoActionMessage message = (ReqDoActionMessage) MessageFactory
				.instance().createMessage(MessageId.REQ_DOACTION);
		message.init(line);
		RespDoActionMessage recvMessage = (RespDoActionMessage) this
				.syncSendMessage(message, (Integer[]) null);
		// try {
		// Thread.sleep(50);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		if (recvMessage.getResult() == 0)
			throw new ErrorActionException("error action:" + line);
		else if (recvMessage.getResult() == 2)
			throw new ReachGoalException("goal reached!");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		RLClientHandler.instance().start();
		((RLClientHandler) RLClientHandler.instance()).init("bin/domain1.pddl",
				"bin/problem1.pddl", null);
	}

}
